package com.shiguiwu.springmybatis.spring.event.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.scheduling.concurrent.ThreadPoolExecutorFactoryBean;
import org.springframework.stereotype.Component;

import java.util.concurrent.Executor;

/**
 * @description:
 * @author: stone
 * @date: Created by 2021/4/24 22:39
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.spring.event.spring
 */
@ComponentScan
@Configuration
public class EventConfig {

    /**
     * 实现事件异步处理
     *
     * @return
     */
    @Bean
    public ApplicationEventMulticaster applicationEventMulticaster() {
        SimpleApplicationEventMulticaster multicaster = new SimpleApplicationEventMulticaster();
        Executor executor = this.applicationEventMulticasterThreadPool().getObject();
        multicaster.setTaskExecutor(executor);
        return multicaster;
    }

    @Bean
    public ThreadPoolExecutorFactoryBean applicationEventMulticasterThreadPool() {
        ThreadPoolExecutorFactoryBean result = new
                ThreadPoolExecutorFactoryBean();
        result.setThreadNamePrefix("applicationEventMulticasterThreadPool-");
        result.setCorePoolSize(5);
        return result;
    }

}
